package com.mjf.cjt.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.mjf.cjt.domain.TCjtSaleOrderDetails;
import com.mjf.cjt.domain.TCjtSaleOrderVo;
import com.mjf.cjt.mapper.SaleOrderMapper;
import com.mjf.cjt.service.AsyncTMService;
import com.mjf.cjt.service.SaleOrderDetailsService;
import com.mjf.cjt.service.SaleOrderService;
import com.mjf.cjt.vo.*;
import com.mjf.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售订单列表查询
 * </p>
 *
 * @author
 * @since 2025-04-05
 */
@Service
public class SaleOrderServiceImpl extends ServiceImpl<SaleOrderMapper, TCjtSaleOrderVo> implements SaleOrderService {
    @Autowired
    private AsyncTMService asyncTMService;

    @Autowired
    private SaleOrderDetailsService saleOrderDetailsService;


    @Override
    public Page<SaleOrderListVo> querySaleOrder(SaleOrderQueryParam param) {

        Page<SaleOrderListVo> page = new Page<>(param.getPage(), param.getSize());
        page.setTotal(0);
        page.setRecords(new ArrayList<>());
        LambdaQueryWrapper<TCjtSaleOrderVo> qw = Wrappers.<TCjtSaleOrderVo>lambdaQuery()
                .ge(StrUtil.isNotBlank(param.getStart()), TCjtSaleOrderVo::getCreatedTime, param.getStart())
                .le(StrUtil.isNotBlank(param.getEnd()), TCjtSaleOrderVo::getCreatedTime, param.getEnd())
                ;
        long count = count(qw);
        page.setTotal(count);
        qw.orderByDesc(TCjtSaleOrderVo::getCreatedTime);
        qw.last("limit " + (param.getPage() - 1) * param.getSize() + "," + param.getSize());

        List<TCjtSaleOrderVo> list = list(qw);
        page.setRecords(list.stream().map(item -> SaleOrderListVo.builder()
                        .id(item.getId())
                        .code(item.getCode())
                        .ts(item.getTs())
                        .voucherDate(item.getVoucherdate())
                        .businessTypeName(item.getBusinessTypeName())
                        .returnOrderTypeName(item.getReturnOrderTypeName())
                        .returnReasonName(item.getReturnReasonName())
                        .returnOrderReason(item.getReturnOrderReason())
                        .customerCode(item.getCustomerCode())
                        .customerName(item.getCustomerName())
                        .customerPartnerAbbName(item.getCustomerPartnerAbbName())
                        .settCusCode(item.getSettCusCode())
                        .settCusName(item.getSettCusName())
                        .settCusPartnerAbbName(item.getSettCusPartnerAbbName())
                        .deptName(item.getDeptName())
                        .clerkName(item.getClerkName())
                        .projectName(item.getProjectName())
                        .warehouseName(item.getWarehouseName())
                        .receWarehouseName(item.getReceWarehouseName())
                        .deliveryDate(item.getDeliverydate())
                        .deliveryModeName(item.getDeliverymodeName())
                        .address(item.getAddress())
                        .provinceName(item.getProvinceName())
                        .cityName(item.getCityName())
                        .districtName(item.getDistrictName())
                        .logisticsName(item.getLogisticsName())
                        .transDocNo(item.getTransdocno())
                        .deliveryCode(item.getDeliverycode())
                        .deliveryVoucherDate(item.getDeliveryVoucherDate())
                        .transDriverName(item.getTransdrivername())
                        .transDriverPhone(item.getTransDriverPhone())
                        .transVehicleInfo(item.getTransvehicleInfo())
                        .logisticsFee(item.getLogisticsFee())
                        .logisticsSettName(item.getLogisticsSettName())
                        .logisticsStatusName(item.getLogisticsStatusName())
                        .linkMan(item.getLinkman())
                        .contactPhone(item.getContactphone())
                        .customerPhone(item.getCustomerphone())
                        .contractCode(item.getContractcode())
                        .reciveTypeName(item.getRecivetypeName())
                        .discountRate(item.getDiscountRate())
                        .dataSourceName(item.getDatasourceName())
                        .externalVoucherCode(item.getExternalVoucherCode())
                        .sourceVoucherTypeCode(item.getSourceVoucherTypeCode())
                        .sourceVoucherTypeName(item.getSourceVoucherTypeName())
                        .voucherStateName(item.getVoucherStateName())
                        .collaborateStateName(item.getCollaborateStateName())
                        .isCounteractOrder(item.getIscounteractOrder())
                        .collaborateVoucherCode(item.getCollaborateVouchercode())
                        .memo(item.getMemo())
                        .maker(item.getMaker())
                        .agency(item.getAgency())
                        .reviser(item.getReviser())
                        .auditor(item.getAuditor())
                        .auditedDate(item.getAuditedDate())
                        .auditedTime(item.getAuditedTime())
                        .printCount(item.getPrintCount())
                        .changer(item.getChanger())
                        .changeDate(item.getChangeDate())
                        .closer(item.getCloser())
                        .closeDate(item.getCloseDate())
                        .createdTime(item.getCreatedTime())
                        .partnerClassName(item.getPartnerClassName())
                        .exchangeRate(item.getExchangeRate())
                        .currencyName(item.getCurrencyName())
                        .memberBalanceIntegral(item.getMemberBalanceIntegral())
                        .memberCode(item.getMemberCode())
                        .memberCardCode(item.getMemberCardcode())
                        .memberName(item.getMemberName())
                        .mobilePhone(item.getMobilePhone())
                        .memberAddress(item.getMemberAddress())
                        .logisticsFee(item.getLogisticsFee())
                        .bomRuleName(item.getBomruleName())
                        .priUserDefNvc1(item.getPriuserdefnvc1())
                        .priUserDefNvc2(item.getPriuserdefnvc2())
                        .priUserDefNvc3(item.getPriuserdefnvc3())
                        .priUserDefNvc4(item.getPriuserdefnvc4())
                        .priUserDefNvc5(item.getPriuserdefnvc5())
                        .priUserDefNvc6(item.getPriuserdefnvc6())
                        .priUserDefDecm1(item.getPriuserdefdecm1())
                        .priUserDefDecm2(item.getPriuserdefdecm2())
                        .priUserDefDecm3(item.getPriuserdefdecm3())
                        .priUserDefDecm4(item.getPriuserdefdecm4())
                        .priUserDefDecm5(item.getPriuserdefdecm5())
                        .priUserDefDecm6(item.getPriuserdefdecm6())
                        .customerPriUserDefDecm1(item.getCustomerPriuserdefdecm1())
                        .customerPriUserDefDecm2(item.getCustomerPriuserdefdecm2())
                        .customerPriUserDefDecm3(item.getCustomerPriuserdefdecm3())
                        .customerPriUserDefDecm4(item.getCustomerPriuserdefdecm4())
                        .customerPriUserDefDecm5(item.getCustomerPriuserdefdecm5())
                        .customerPriUserDefNvc1(item.getCustomerPriuserdefnvc1())
                        .customerPriUserDefNvc2(item.getCustomerPriuserdefnvc2())
                        .customerPriUserDefNvc3(item.getCustomerPriuserdefnvc3())
                        .customerPriUserDefNvc4(item.getCustomerPriuserdefnvc4())
                        .customerPriUserDefNvc5(item.getCustomerPriuserdefnvc5())
                        .origAmount(item.getOrigamount())
                        .origTaxAmount(item.getOrigtaxamount())
                        .amount(item.getAmount())
                        .taxAmount(item.getTaxamount())
                        .idMarketingOrgan(item.getIdmarketingOrgan())
                        .build())
                .collect(Collectors.toList()));
        return page;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getSaleOrderFromRemote(String start, String end) {
        try {
            List<SaleOrderVo> saleOrderRemote = asyncTMService.getSaleOrderFromRemote(start, end);
            if (CollectionUtil.isNotEmpty(saleOrderRemote)) {
                // 按明细行ID分组
                Map<Long, List<SaleOrderVo>> voMap =
                        saleOrderRemote.stream().collect(Collectors.groupingBy(SaleOrderVo::getId));

                List<TCjtSaleOrderVo> saleOrderPageList = new ArrayList<>();

                List<TCjtSaleOrderDetails> saleOrderDetailsList = new ArrayList<>();

                for (Map.Entry<Long, List<SaleOrderVo>> entry : voMap.entrySet()) {

                    // 主单
                    TCjtSaleOrderVo order = new TCjtSaleOrderVo();
                    order.setId(entry.getValue().get(0).getId());
                    order.setCode(entry.getValue().get(0).getCode());
                    order.setTs(entry.getValue().get(0).getTs());
                    order.setVoucherdate(StringUtils.isNotEmpty(entry.getValue().get(0).getVoucherdate())?
                            LocalDate.parse(entry.getValue().get(0).getVoucherdate().replaceAll("/","-")) : null);
                    order.setBusinessTypeName(entry.getValue().get(0).getBusinesstype_name());
                    order.setReturnOrderTypeName(entry.getValue().get(0).getReturnordertype_name());
                    order.setReturnReasonName(entry.getValue().get(0).getReturnreason_name());
                    order.setReturnOrderReason(entry.getValue().get(0).getReturnorderreason());
                    order.setCustomerCode(entry.getValue().get(0).getCustomer_code());
                    order.setCustomerName(entry.getValue().get(0).getCustomer_name());
                    order.setCustomerPartnerAbbName(entry.getValue().get(0).getCustomer_partnerabbname());
                    order.setSettCusCode(entry.getValue().get(0).getSettlecustomer_code());
                    order.setSettCusName(entry.getValue().get(0).getSettlecustomer_name());
                    order.setSettCusPartnerAbbName(entry.getValue().get(0).getSettlecustomer_partnerabbname());
                    order.setDeptName(entry.getValue().get(0).getDepartment_name());
                    order.setClerkName(entry.getValue().get(0).getClerk_name());
                    order.setProjectName(entry.getValue().get(0).getProject_name());
                    order.setWarehouseName(entry.getValue().get(0).getWarehouse_name());
                    order.setReceWarehouseName(entry.getValue().get(0).getReceivewarehouse_name());
                    order.setDeliverydate(entry.getValue().get(0).getDeliverydate());
                    order.setDeliverymodeName(entry.getValue().get(0).getDeliverymode_name());
                    order.setAddress(entry.getValue().get(0).getAddress());
                    order.setProvinceName(entry.getValue().get(0).getProvince_name());
                    order.setCityName(entry.getValue().get(0).getCity_name());
                    order.setDistrictName(entry.getValue().get(0).getDistrict_name());
                    order.setLogisticsName(entry.getValue().get(0).getLogisticsnetwork_name());
                    order.setTransdocno(entry.getValue().get(0).getTransdocno());
                    order.setDeliverycode(entry.getValue().get(0).getDeliverycode());
                    order.setDeliveryVoucherDate(entry.getValue().get(0).getDeliveryvoucherdate());
                    order.setTransDriverPhone(entry.getValue().get(0).getTransdriverphone());
                    order.setLogisticsSettName(entry.getValue().get(0).getLogisticssettlementmodes_name());
                    order.setTransvehicleInfo(entry.getValue().get(0).getTransvehicleinfo());
                    order.setTransdrivername(entry.getValue().get(0).getTransdrivername());
                    order.setLogisticsStatusName(entry.getValue().get(0).getLogisticsstatus_name());
                    order.setLinkman(entry.getValue().get(0).getLinkman());
                    order.setContactphone(entry.getValue().get(0).getContactphone());
                    order.setCustomerphone(entry.getValue().get(0).getCustomerphone());
                    order.setContractcode(entry.getValue().get(0).getContractcode());
                    order.setRecivetypeName(entry.getValue().get(0).getRecivetype_name());
                    order.setDiscountRate(entry.getValue().get(0).getDiscountrate());
                    order.setDatasourceName(entry.getValue().get(0).getDatasource_name());
                    order.setExternalVoucherCode(entry.getValue().get(0).getExternalvouchercode());
                    order.setSourceVoucherTypeCode(entry.getValue().get(0).getSourcevouchercode());
                    order.setSourceVoucherTypeName(entry.getValue().get(0).getSourcevouchertype_name());
                    order.setVoucherStateName(entry.getValue().get(0).getVoucherstate_name());
                    order.setCollaborateStateName(entry.getValue().get(0).getSocialcollaboratestatus_name());
                    order.setIscounteractOrder(entry.getValue().get(0).getIscounteractorder());
                    order.setCollaborateVouchercode(entry.getValue().get(0).getCollaboratevouchercode());
                    order.setMemo(entry.getValue().get(0).getMemo());
                    order.setMaker(entry.getValue().get(0).getMaker());
                    order.setAgency(entry.getValue().get(0).getAgency());
                    order.setReviser(entry.getValue().get(0).getReviser());
                    order.setAuditor(entry.getValue().get(0).getAuditor());
                    order.setAuditedDate(entry.getValue().get(0).getAuditeddate());
                    order.setAuditedTime(entry.getValue().get(0).getAuditedtime());
                    order.setPrintCount(entry.getValue().get(0).getPrintcount());
                    order.setChanger(entry.getValue().get(0).getChanger());
                    order.setChangeDate(entry.getValue().get(0).getChangedate());
                    order.setCloser(entry.getValue().get(0).getCloser());
                    order.setCloseDate(entry.getValue().get(0).getClosedate());
                    order.setCreatedTime(entry.getValue().get(0).getCreatedtime());
                    order.setPartnerClassName(entry.getValue().get(0).getCustomer_partnerclass_name());
                    order.setExchangeRate(entry.getValue().get(0).getExchangerate());
                    order.setCurrencyName(entry.getValue().get(0).getCurrency_name());
                    order.setMemberBalanceIntegral(entry.getValue().get(0).getMember_balanceintegral());
                    order.setMemberCode(entry.getValue().get(0).getMember_code());
                    order.setMemberCardcode(entry.getValue().get(0).getMember_cardcode());
                    order.setMemberName(entry.getValue().get(0).getMember_name());
                    order.setMobilePhone(entry.getValue().get(0).getMobilephone());
                    order.setMemberAddress(entry.getValue().get(0).getMemberaddress());
                    order.setLogisticsFee(Objects.isNull(entry.getValue().get(0).getLogisticsfee())? null : new BigDecimal(entry.getValue().get(0).getLogisticsfee()));
                    order.setBomruleName(entry.getValue().get(0).getBomrule_name());
                    order.setPriuserdefnvc1(entry.getValue().get(0).getPriuserdefnvc1());
                    order.setPriuserdefnvc2(entry.getValue().get(0).getPriuserdefnvc2());
                    order.setPriuserdefnvc3(entry.getValue().get(0).getPriuserdefnvc3());
                    order.setPriuserdefnvc4(entry.getValue().get(0).getPriuserdefnvc4());
                    order.setPriuserdefnvc5(entry.getValue().get(0).getPriuserdefnvc5());
                    order.setPriuserdefnvc6(entry.getValue().get(0).getPriuserdefnvc6());
                    order.setPriuserdefdecm1(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm1())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm1()));
                    order.setPriuserdefdecm2(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm2())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm2()));
                    order.setPriuserdefdecm3(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm3())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm3()));
                    order.setPriuserdefdecm4(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm4())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm4()));
                    order.setPriuserdefdecm5(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm5())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm5()));
                    order.setPriuserdefdecm6(Objects.isNull(entry.getValue().get(0).getPriuserdefdecm6())? null : new BigDecimal(entry.getValue().get(0).getPriuserdefdecm6()));
                    order.setCustomerPriuserdefdecm1(Objects.isNull(entry.getValue().get(0).getCustomer_priuserdefdecm1())? null : new BigDecimal(entry.getValue().get(0).getCustomer_priuserdefdecm1()));
                    order.setCustomerPriuserdefdecm2(Objects.isNull(entry.getValue().get(0).getCustomer_priuserdefdecm2())? null : new BigDecimal(entry.getValue().get(0).getCustomer_priuserdefdecm2()));
                    order.setCustomerPriuserdefdecm3(Objects.isNull(entry.getValue().get(0).getCustomer_priuserdefdecm3())? null : new BigDecimal(entry.getValue().get(0).getCustomer_priuserdefdecm3()));
                    order.setCustomerPriuserdefdecm4(Objects.isNull(entry.getValue().get(0).getCustomer_priuserdefdecm4())? null : new BigDecimal(entry.getValue().get(0).getCustomer_priuserdefdecm4()));
                    order.setCustomerPriuserdefdecm5(Objects.isNull(entry.getValue().get(0).getCustomer_priuserdefdecm5())? null : new BigDecimal(entry.getValue().get(0).getCustomer_priuserdefdecm5()));
                    order.setCustomerPriuserdefnvc1(entry.getValue().get(0).getCustomer_priuserdefdecm1());
                    order.setCustomerPriuserdefnvc2(entry.getValue().get(0).getCustomer_priuserdefdecm2());
                    order.setCustomerPriuserdefnvc3(entry.getValue().get(0).getCustomer_priuserdefdecm3());
                    order.setCustomerPriuserdefnvc4(entry.getValue().get(0).getCustomer_priuserdefdecm4());
                    order.setCustomerPriuserdefnvc5(entry.getValue().get(0).getCustomer_priuserdefdecm5());
                    order.setOrigamount(entry.getValue().get(0).getOrigamount());
                    order.setOrigtaxamount(entry.getValue().get(0).getOrigtaxamount());
                    order.setAmount(entry.getValue().get(0).getAmount());
                    order.setTaxamount(entry.getValue().get(0).getTaxamount());
                    order.setIdmarketingOrgan(entry.getValue().get(0).getIdmarketingorgan());
                    saleOrderPageList.add(order);

                    // 按明细ID分组
                    Map<Long, List<SaleOrderVo>> detailsMap = entry.getValue().stream().collect(Collectors.groupingBy(SaleOrderVo::getSaleorderdetails_id));
                    for (Map.Entry<Long, List<SaleOrderVo>> detailsEntry : detailsMap.entrySet()) {
                        // 明细
                        SaleOrderVo orderDetail = detailsEntry.getValue().get(0);
                        TCjtSaleOrderDetails details = new TCjtSaleOrderDetails();
                        details.setSaleorderDetailsId(orderDetail.getSaleorderdetails_id());
                        details.setSaleorderId(orderDetail.getId());
                        details.setWarehouseName(orderDetail.getWarehouse_name());
                        details.setProjectName(orderDetail.getProject_name());
                        details.setPartnerInventoryName(orderDetail.getSaleorderdetails_partnerinventoryname());
                        details.setPartnerInventoryCode(orderDetail.getSaleorderdetails_partnerinventorycode());
                        details.setInventoryName(orderDetail.getSaleorderdetails_inventory_name());
                        details.setInventorclassName(orderDetail.getSaleorderdetails_inventory_inventoryclass_name());
                        details.setInventoryCode(orderDetail.getSaleorderdetails_inventory_code());
                        details.setInventorySpecification(orderDetail.getSaleorderdetails_inventory_specification());
                        details.setInventoryPriuserdefnvc1(orderDetail.getSaleorderdetails_inventory_priuserdefnvc1());
                        details.setInventoryPriuserdefnvc2(orderDetail.getSaleorderdetails_inventory_priuserdefnvc2());
                        details.setInventoryPriuserdefnvc3(orderDetail.getSaleorderdetails_inventory_priuserdefnvc3());
                        details.setInventoryPriuserdefnvc4(orderDetail.getSaleorderdetails_inventory_priuserdefnvc4());
                        details.setInventoryPriuserdefnvc5(orderDetail.getSaleorderdetails_inventory_priuserdefnvc5());
                        details.setInventoryPriuserdefdecm1(orderDetail.getSaleorderdetails_inventory_priuserdefdecm1());
                        details.setInventoryPriuserdefdecm2(orderDetail.getSaleorderdetails_inventory_priuserdefdecm2());
                        details.setInventoryPriuserdefdecm3(orderDetail.getSaleorderdetails_inventory_priuserdefdecm3());
                        details.setInventoryPriuserdefdecm4(orderDetail.getSaleorderdetails_inventory_priuserdefdecm4());
                        details.setInventoryPriuserdefdecm5(orderDetail.getSaleorderdetails_inventory_priuserdefdecm5());
                        details.setUnitName(orderDetail.getSaleorderdetails_unit_name());
                        details.setQuantity(orderDetail.getSaleorderdetails_quantity());
                        details.setUnit2Name(orderDetail.getSaleorderdetails_unit2_name());
                        details.setUnitexchangerate(orderDetail.getSaleorderdetails_unitexchangerate());
                        details.setQuantity2(orderDetail.getSaleorderdetails_quantity2());
                        details.setCompositionQuantity(orderDetail.getSaleorderdetails_compositionquantity());
                        details.setRetailprice(orderDetail.getSaleorderdetails_retailprice());
                        details.setLatestPorigTaxprice(orderDetail.getSaleorderdetails_latestporigtaxprice());
                        details.setLatestSaleorigTaxprice(orderDetail.getSaleorderdetails_latestsaleorigtaxprice());
                        details.setLowestSaleprice(orderDetail.getSaleorderdetails_lowestsaleprice());
                        details.setSingleInvgrossProfit(orderDetail.getSaleorderdetails_singleinvgrossprofit());
                        details.setGrossProfit(orderDetail.getSaleorderdetails_grossprofit());
                        details.setGrossProfitRate(Objects.isNull(orderDetail.getSaleorderdetails_grossprofitrate())? null :new BigDecimal(orderDetail.getSaleorderdetails_grossprofitrate()));
                        details.setOrigprice(orderDetail.getSaleorderdetails_origprice());
                        details.setDiscountrate(Objects.isNull(orderDetail.getSaleorderdetails_discountrate())? null :new BigDecimal(orderDetail.getSaleorderdetails_discountrate()));
                        details.setTaxrate(Objects.isNull(orderDetail.getSaleorderdetails_taxrate()) ? null : new BigDecimal(orderDetail.getSaleorderdetails_taxrate()));
                        details.setOrigDiscountPrice(orderDetail.getSaleorderdetails_origdiscountprice());
                        details.setOrigTaxPrice(orderDetail.getSaleorderdetails_origtaxprice());
                        details.setOrigDiscountAmount(orderDetail.getSaleorderdetails_origdiscountamount());
                        details.setOrigTax(orderDetail.getSaleorderdetails_origtax());
                        details.setOrigTaxAmount(orderDetail.getSaleorderdetails_origtaxamount());
                        details.setOrigDiscount(orderDetail.getSaleorderdetails_origdiscount());
                        details.setTotalCost(orderDetail.getSaleorderdetails_totalcost());
                        details.setCostUnitPrice(orderDetail.getSaleorderdetails_costunitprice());
                        details.setSalesCostUnitPrice(orderDetail.getSaleorderdetails_salescostunitprice());
                        details.setMarkupRate(orderDetail.getSaleorderdetails_markuprate());
                        details.setMarkupAmount(orderDetail.getSaleorderdetails_markupamount());
                        details.setDiscountPrice(orderDetail.getSaleorderdetails_discountprice());
                        details.setTaxPrice(orderDetail.getSaleorderdetails_taxprice());
                        details.setDiscountAmount(orderDetail.getSaleorderdetails_discountamount());
                        details.setTax(orderDetail.getSaleorderdetails_tax());
                        details.setTaxAmount(orderDetail.getSaleorderdetails_taxamount());
                        details.setDiscount(orderDetail.getSaleorderdetails_discount());
                        details.setDeliverydate(orderDetail.getSaleorderdetails_deliverydate());
                        details.setIspresent(orderDetail.getSaleorderdetails_ispresent());
                        details.setIsclose(orderDetail.getSaleorderdetails_isclose());
                        details.setCloser(orderDetail.getSaleorderdetails_closer());
                        details.setDatasourceName(orderDetail.getSaleorderdetails_datasource_name());
                        details.setSourceVouchertypeName(orderDetail.getSaleorderdetails_sourcevouchertype_name());
                        details.setSourceVouchercode(orderDetail.getSaleorderdetails_sourcevouchercode());
                        details.setPriuserdefnvc1(orderDetail.getSaleorderdetails_priuserdefnvc1());
                        details.setPriuserdefnvc2(orderDetail.getSaleorderdetails_priuserdefnvc2());
                        details.setPriuserdefnvc3(orderDetail.getSaleorderdetails_priuserdefnvc3());
                        details.setPriuserdefnvc4(orderDetail.getSaleorderdetails_priuserdefnvc4());
                        details.setPriuserdefdecm1(Objects.isNull(orderDetail.getSaleorderdetails_priuserdefdecm1())? null :new BigDecimal(orderDetail.getSaleorderdetails_priuserdefdecm1()));
                        details.setPriuserdefdecm2(Objects.isNull(orderDetail.getSaleorderdetails_priuserdefdecm2())? null :new BigDecimal(orderDetail.getSaleorderdetails_priuserdefdecm2()));
                        details.setPriuserdefdecm3(Objects.isNull(orderDetail.getSaleorderdetails_priuserdefdecm3())? null :new BigDecimal(orderDetail.getSaleorderdetails_priuserdefdecm3()));
                        details.setPriuserdefdecm4(Objects.isNull(orderDetail.getSaleorderdetails_priuserdefdecm4())? null :new BigDecimal(orderDetail.getSaleorderdetails_priuserdefdecm4()));
                        details.setPubuserdefnvc1(orderDetail.getSaleorderdetails_pubuserdefnvc1());
                        details.setPubuserdefnvc2(orderDetail.getSaleorderdetails_pubuserdefnvc2());
                        details.setPubuserdefnvc3(orderDetail.getSaleorderdetails_pubuserdefnvc3());
                        details.setPubuserdefnvc4(orderDetail.getSaleorderdetails_pubuserdefnvc4());
                        details.setPubuserdefdecm1(orderDetail.getSaleorderdetails_pubuserdefdecm1());
                        details.setPubuserdefdecm2(orderDetail.getSaleorderdetails_pubuserdefdecm2());
                        details.setPubuserdefdecm3(orderDetail.getSaleorderdetails_pubuserdefdecm3());
                        details.setPubuserdefdecm4(orderDetail.getSaleorderdetails_pubuserdefdecm4());
                        details.setDetailmemo(orderDetail.getDetailmemo());
                        saleOrderDetailsList.add(details);

                    }
                }
                saveOrUpdateBatch(saleOrderPageList);
                saleOrderDetailsService.saveOrUpdateBatch(saleOrderDetailsList);
            }
        } catch (ChanjetApiException e) {
            throw new RuntimeException(e);
        }

    }
}
